package net.zoneland.knowledge.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;

import net.zoneland.knowledge.cache.RedisCacheUtil;
import net.zoneland.knowledge.dao.DictItemMapper;
import net.zoneland.knowledge.enums.CacheEnum;
import net.zoneland.knowledge.model.DictItem;
import net.zoneland.knowledge.model.DictItemExample;
import net.zoneland.knowledge.service.DictItemService;
import net.zoneland.knowledge.utils.GsonUtils;

/**
 * .
 *
 * @author: bcc
 * 2021/5/26
 */
@Service
public class DictItemServiceImpl implements DictItemService {

    /**
     * logger.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(DictItemServiceImpl.class);

    @Autowired
    private DictItemMapper dictItemMapper;

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Override
    public DictItem getByValue(final String parentId, final String value) {
        List<DictItem> dicList = getCache(parentId);
        if (null != dicList && dicList.size() > 0) {
            for (DictItem item:dicList) {
                if (parentId.equals(item.getParentId()) && item.getDictValue().equals(value)) {
                    return item;
                }
            }
        }
        return null;
    }

    @Override
    public List<DictItem> findDictByParentId(final String parentId) {
        return getCache(parentId);
    }

    @Override
    public List<DictItem> findDictByParentIdAndDictName(final String parentId, final String dictName) {
        List<DictItem> rs = new ArrayList<>();
        List<DictItem> dicList = getCache(parentId);
        if (null != dicList && dicList.size() > 0) {
            for (DictItem item:dicList) {
                if (parentId.equals(item.getParentId()) && item.getDictName().equals(dictName)) {
                    rs.add(item);
                }
            }
        }
        return rs;
    }

    /**
     * 单独加载某一项数据字典缓存.
     *
     * @throws Exception
     */
    private List<DictItem> getCache(final String parentId) {
        List<DictItem> list = null;
        try {
            String dicJson = redisCacheUtil.hget(CacheEnum.DICT_CACHE_PREFIX.getValue(), parentId);
            if (StringUtils.isNotBlank(dicJson)) {
                list = GsonUtils.GSON.fromJson(dicJson, new TypeToken<List<DictItem>>() { }.getType());
            }
        } catch (Exception e) {
            LOGGER.error("{} redis cache read fail, parentId:{}", CacheEnum.DICT_CACHE_PREFIX.getValue(), parentId, e);
        }
        if (CollectionUtils.isEmpty(list)) {
            final DictItemExample example = new DictItemExample();
            example.createCriteria().andParentIdEqualTo(parentId);
            example.setOrderByClause("orders ASC");
            list = dictItemMapper.selectByExample(example);
        }
        return list;
    }
}
